/* 
 * File:   Ticker.h
 * Author: Julian Sanin
 *
 * Created on 01/02/2014, 20:06
 * Ported mbed ticker api to PIC24.
 * Sources: Ticker.h,
 *          Ticker.cpp
 */

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef TICKER_H
#define	TICKER_H

#ifdef	__cplusplus
extern "C" {
#endif

#include "TimerEvent.h"

/**
 * A Ticker is used to call a function at a recurring interval.
 * You can use as many seperate Ticker objects as you require.
 * Example:
 * @code
 * #include <xc.h>
 * #include "Ticker.h"
 *
 * Ticker ticker;
 *
 * void togglePin() {
 *     LATAbits.LATA0 ^= 1;
 * }
 * 
 * int main(void) {
 *     TRISAbits.TRISA0 = 0;
 *     Ticker_attach(&ticker, &toggle, 1000);
 *     while(1);
 * }
 * @endcode
 */
CLASS_EXTEND(Ticker, TimerEvent)
    unsigned int _delay;
    void (*_function)(void);
OVERRIDE(Ticker, TimerEvent)
    void (*dtor)(Ticker *);
    void (*onEvent)(Ticker *);
METHODS
    void Ticker_ctor(Ticker *obj);
    void Ticker_dtor(Ticker *obj);
    /**
     * @brief Attach a function to be called by the Ticker, specifiying the
     * interval in milli seconds.
     * @param[in]  obj    The Ticker object.
     * @param[in]  func   The function to be called after millis time.
     * @param[in]  millis The time between the function call.
     */
    void Ticker_attach(Ticker *obj, void (*func)(void), unsigned int millis);
    /**
     * Detach the registered function from the Ticker object.
     * @param[in]  obj The Ticker object.
     */
    void Ticker_detach(Ticker *obj);
    void _Ticker_onEvent(Ticker *obj);
CLASS_END

#ifdef	__cplusplus
}
#endif

#endif	/* TICKER_H */
